package com.yjhb.android.sbz.adapter

import android.content.Context
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.yjhb.android.sbz.Conf
import com.yjhb.android.sbz.R
import com.yjhb.android.sbz.entity.ExportData
import com.yjhb.android.sbz.tool.LoadingDialogTool
import com.yjhb.android.sbz.tool.Tool
import com.jojo.android.zxlib.base.BaseRecyclerAdapter
import com.jojo.android.zxlib.tool.*
import java.io.File


/**
 *  创建者 zx
 *  创建时间 2021/3/18 9:18
 */
class ExportAdapter(context: Context) : BaseRecyclerAdapter<ExportAdapter.Holder, ExportData>(context) {


    override fun getLayoutId() = R.layout.item_export

    override fun onBindViewHolder(holder: Holder, position: Int) {
        val entity = data[position]
        holder.nameTv?.text = entity.fileName
        holder.timeTv?.text = ZxTimeTool.date2String(entity.createTime)
        val file = File(Conf.FILE_SAVE_PATH + ZxEncryptTool.encryptMD5ToString(entity.url) + ZxFileTool.getUrlSuffix(entity.url,".png"))
        if (file.exists()) {
            holder.downloadTv?.let {
                it.text = "打开"
                it.setOnClickListener {
                    val intent = ZxFileHelper.getIntent(file)
                    if (intent == null) {
                        ZxToast.showINFOBottomToast("文件不存在！请刷新后重新下载！")
                        return@setOnClickListener
                    }
                    context.startActivity(intent)
                }
            }
        } else {
            holder.downloadTv?.let {
                it.text = "下载"
                it.setOnClickListener {
                    val loading = LoadingDialogTool.createDialog(context)
                    loading.setTipsText("")
                    loading.show()
                    Tool.saveFile(entity.url, Conf.FILE_SAVE_PATH) { t ->
                        t?.run {
                            if (this.code == 200) {
                                val intent = ZxFileHelper.getIntent(File(this.data))
                                context.startActivity(intent)
                                notifyItemChanged(position)
                            } else {
                                ZxToast.showINFOBottomToast(this.message)
                            }
                        }
                        loading.cancel()
                    }
                }
            }
        }

    }

    override fun getHolder(v: View): Holder {
        return Holder(v)
    }

    inner class Holder(itemView: View) : RecyclerView.ViewHolder(itemView) {

        var nameTv: TextView? = null
        var iconIv: ImageView? = null
        var timeTv: TextView? = null
        var lengthTv: TextView? = null
        var downloadTv: TextView? = null

        init {
            nameTv = itemView.findViewById(R.id.name_tv)
            iconIv = itemView.findViewById(R.id.icon_iv)
            timeTv = itemView.findViewById(R.id.time_tv)
            lengthTv = itemView.findViewById(R.id.length_tv)
            downloadTv = itemView.findViewById(R.id.download_tv)
        }

    }
}